package com.netintech.process.todoitem.controller;

import com.netintech.common.utils.SecurityUtils;
import com.netintech.common.utils.poi.ExcelUtil;
import com.netintech.core.aspectj.lang.annotation.Log;
import com.netintech.core.aspectj.lang.enums.BusinessType;
import com.netintech.core.web.controller.BaseController;
import com.netintech.core.web.domain.RequestResult;
import com.netintech.core.web.page.TableDataInfo;
import com.netintech.process.leave.domain.BizLeaveVo;
import com.netintech.process.leave.service.IBizLeaveService;
import com.netintech.process.service.ProcessCommonService;
import com.netintech.process.todoitem.domain.BizTodoItem;
import com.netintech.process.todoitem.service.IBizTodoItemService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待办事项Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/process")
public class BizTodoItemController extends BaseController {

    private String prefix = "process/todoitem";

    private static final String HAVE_NOT_HANDLE = "0";

    private static final String HAVE_HANDLE = "1";

    private static final String ADMIN_ID = "1";

    @Autowired
    private IBizTodoItemService bizTodoItemService;

    @Autowired
    private IBizLeaveService bizLeaveService;

    @Autowired
    private ProcessCommonService processCommonService;

    /**
     * 查询所有待办数据
     * @return
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:toDoList')")
    @GetMapping("/todoList")
    public TableDataInfo todoListView(BizTodoItem todoItem) {
        setPageParam();
        String userId = SecurityUtils.getUserIdString();
        if(!ADMIN_ID.equals(userId)){
            todoItem.setTodoUserId(SecurityUtils.getUserIdString());
        }
        todoItem.setIsHandle(HAVE_NOT_HANDLE);
        return getDataTable(bizTodoItemService.selectBizTodoItemList(todoItem));
    }

    /**
     * 加载办理弹窗数据
     * @param taskId
     * @return
     */
    @GetMapping("/showVerifyDialog/{taskId}")
    public RequestResult showVerifyDialog(@PathVariable("taskId") String taskId) {
        Map<Object, Object> map = new HashMap<>();
        Map<String, String> stringMap = processCommonService.showAuditPage(taskId);
        BizLeaveVo bizLeave = bizLeaveService.selectBizLeaveById(new Long(stringMap.get("entityId")));
        map.put("bizLeave", bizLeave);
        map.put("taskId", taskId);
        return RequestResult.success(map);
    }


    /**
     * 查询已办事项列表
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:doneList')")
    @GetMapping("/doneList")
    public TableDataInfo doneList(BizTodoItem bizTodoItem) {
        setPageParam();
        String userId = SecurityUtils.getUserIdString();
        if (!ADMIN_ID.equals(userId)) {
            bizTodoItem.setTodoUserId(SecurityUtils.getUserIdString());
        }
        bizTodoItem.setIsHandle(HAVE_HANDLE);
        return getDataTable(bizTodoItemService.selectBizTodoItemList(bizTodoItem));
    }

    /**
     * 导出待办事项列表
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:export')")
    @GetMapping("/toDoExport")
    @ResponseBody
    public RequestResult export(BizTodoItem bizTodoItem) {
        bizTodoItem.setIsHandle(HAVE_NOT_HANDLE);
        List<BizTodoItem> list = bizTodoItemService.selectBizTodoItemList(bizTodoItem);
        ExcelUtil<BizTodoItem> util = new ExcelUtil<BizTodoItem>(BizTodoItem.class);
        return util.exportExcel(list, "待办事项");
    }

    /**
     * 导出已办事项列表
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:doneExport')")
    @GetMapping("/doneExport")
    @ResponseBody
    public RequestResult doneExport(BizTodoItem bizTodoItem) {
        bizTodoItem.setIsHandle(HAVE_HANDLE);
        List<BizTodoItem> list = bizTodoItemService.selectBizTodoItemList(bizTodoItem);
        ExcelUtil<BizTodoItem> util = new ExcelUtil<BizTodoItem>(BizTodoItem.class);
        return util.exportExcel(list, "已办事项");
    }

    /**
     * 新增保存待办事项
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:add')")
    @Log(title = "待办事项", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public RequestResult addSave(BizTodoItem bizTodoItem) {
        return returnResult(bizTodoItemService.insertBizTodoItem(bizTodoItem));
    }

    /**
     * 修改待办事项
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizTodoItem bizTodoItem = bizTodoItemService.selectBizTodoItemById(id);
        mmap.put("bizTodoItem", bizTodoItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存待办事项
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:edit')")
    @Log(title = "待办事项", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public RequestResult editSave(BizTodoItem bizTodoItem) {
        return returnResult(bizTodoItemService.updateBizTodoItem(bizTodoItem));
    }

    /**
     * 删除待办事项
     */
    @PreAuthorize("@ss.hasPermi('process:todoitem:remove')")
    @Log(title = "待办事项", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public RequestResult remove(String ids) {
        return returnResult(bizTodoItemService.deleteBizTodoItemByIds(ids));
    }
}
